﻿using System;
using System.CodeDom;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls.Primitives;
using System.Windows.Media;
using System.Windows.Controls;
using System.Windows.Data;

namespace WpfCustomControlLibrary3
{
    [TemplatePart(Name = "PART_RedSlider", Type = typeof(RangeBase))]
    [TemplatePart(Name = "PART_Brush", Type = typeof(SolidColorBrush))]
    public class ColorPicker : Control
    {
        #region 01 - 属性和依赖项属性
        public static readonly DependencyProperty ColorProperty;
        public static readonly DependencyProperty RedProperty;
        public static readonly DependencyProperty GreenProperty;
        public static readonly DependencyProperty BlueProperty;

        //Color 颜色
        public Color Color
        {
            get => (Color)GetValue(ColorProperty);
            set { SetValue(ColorProperty, value); }
        }

        public byte Red { get => (byte)GetValue(RedProperty); set { SetValue(RedProperty, value); } }
        public byte Green { get => (byte)GetValue(GreenProperty); set { SetValue(GreenProperty, value); } }
        public byte Blue { get => (byte)GetValue(BlueProperty); set { SetValue(BlueProperty, value); } }

        #endregion

        #region 02 - 事件
        public static readonly RoutedEvent ColorChangeEvent;
        public event RoutedPropertyChangedEventHandler<Color> ColorChanged
        {
            add { AddHandler(ColorChangeEvent, value); }
            remove { RemoveHandler(ColorChangeEvent, value); }
        }

        #endregion

        #region 03 - 构造函数
        static ColorPicker()
        {
            //从样式中，加载模板
            DefaultStyleKeyProperty.OverrideMetadata(typeof(ColorPicker), new FrameworkPropertyMetadata(typeof(ColorPicker)));


            #region 01-注册依赖项属性
            ColorProperty = DependencyProperty.Register("Color", typeof(Color),
                typeof(ColorPicker),
                new(OnColorChanged) { DefaultValue = Colors.Black });

            RedProperty = DependencyProperty.Register("Red",
                typeof(byte),
                typeof(ColorPicker),
                new(OnRGBChanged));

            GreenProperty = DependencyProperty.Register("Green",
                typeof(byte),
                typeof(ColorPicker),
                new(OnRGBChanged));

            BlueProperty = DependencyProperty.Register("Blue",
                typeof(byte),
                typeof(ColorPicker),
                new(OnRGBChanged));
            #endregion

            #region 02-注册路由事件
            ColorChangeEvent = EventManager.RegisterRoutedEvent("ColorChanged", RoutingStrategy.Bubble,
                typeof(RoutedPropertyChangedEventHandler<Color>), typeof(ColorPicker));
            #endregion
        }

        private static void OnColorChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            ColorPicker colorPicker = (ColorPicker)sender;

            Color newColor = (Color)e.NewValue;
            Color oldColor = (Color)e.OldValue;

            //同样需要解析Color到RGB
            colorPicker.Red = newColor.R;
            colorPicker.Green = newColor.G;
            colorPicker.Blue = newColor.B;

            //要在颜色改变之后，调用事件
            RoutedPropertyChangedEventArgs<Color> args = new(oldColor, newColor) { RoutedEvent = ColorChangeEvent };
            colorPicker.RaiseEvent(args);
        }

        private static void OnRGBChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            ColorPicker colorPicker = (ColorPicker)sender;
            Color color = colorPicker.Color;

            if (e.Property == RedProperty)
            {
                color.R = colorPicker.Red;
            }
            else if (e.Property == GreenProperty)
            {
                color.G = colorPicker.Green;
            }
            else if (e.Property == BlueProperty)
            {
                color.B = colorPicker.Blue;
            }

            colorPicker.Color = color;
        }

        /// <summary>
        /// 当DefaultStyleKeyProperty.OverrideMetadata初始化完成Style的时候回调
        /// </summary>
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            //手动建立数据绑定
            RangeBase? redSlider = GetTemplateChild("PART_RedSlider") as RangeBase;

            Binding binding = new("Red")
            {
                Source = this,
                Mode = BindingMode.TwoWay,
            };

            if (redSlider != null)
            {
                redSlider.SetBinding(RangeBase.ValueProperty, binding);
            }

            //建立Brush的绑定
            SolidColorBrush? brush = GetTemplateChild("PART_Brush") as SolidColorBrush;
            Binding binding1 = new("Color")
            {
                Mode = BindingMode.OneWayToSource,
                Source = brush
            };

            this.SetBinding(ColorProperty, binding1);

        }

        #endregion
    }
}
